﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wise.Entities;
using Wise.Util;

namespace NHI.Sync.Tools.Forms
{
    /// <summary>
    /// Execute.xaml 的交互逻辑
    /// </summary>
    public partial class Form_Execute : UserControl
    {

        public static Form_Execute Current { get; private set; }


        public Form_Execute()
        {
            InitializeComponent();
            MyStatus = new Form_Execute_Status();
            MyStatus_Copy = new Forms.Form_Execute_Status();
            MyOperators = new List<Form_Execute_Operator>(); ;
            MyOperators_Copy = new List<Forms.Form_Execute_Operator>();
            Current = this;
            _Bind_Tread = new Thread(BindData);
        }

        public Thread _Bind_Tread = null;

        public bool ShowErrorOnly = false;
        public bool ShowSuccessOnly = false;
        public bool ShowWarningOnly = false;

        public void BindData(Object stopFlag)
        {



            //int times = 3;
            bool flag = (stopFlag as bool?) ?? false;
            flag = !flag;
            do
            {
                try
                {
                    Dispatcher.Invoke(() =>
                    {
                        lblExecuteStatus.Content = MyStatus.StatusText;
                        (imgExecuteStatus.Child as System.Windows.Forms.PictureBox).ImageLocation = FileHelper.ToAbsolute($"Styles/{MyStatus.StatusText}.gif");
                        switch (MyStatus.Status)
                        {
                            case Form_Execute_StatusType.运行中:
                                lblExecuteStatus.Content = $"剩余{Form_Execute.Current.MyStatus.Total - Form_Execute.Current.MyStatus.Successed - Form_Execute.Current.MyStatus.Warning - Form_Execute.Current.MyStatus.Error}";
                                break;
                        }
                        MyStatus.CopyTo(MyStatus_Copy);
                        MyOperators_Copy.Clear();
                        lock (MyOperators)
                        {
                            MyOperators_Copy.AddRange(MyOperators.Select(x => x.ChangeTo<Form_Execute_Operator>()));
                        }
                        if (ShowErrorOnly)
                        {
                            MyOperators_Copy.RemoveAll(x => x.Error == 0);
                        }
                        if (ShowWarningOnly)
                        {
                            MyOperators_Copy.RemoveAll(x => x.Warning == 0);
                        }
                        if (ShowSuccessOnly)
                        {
                            MyOperators_Copy.RemoveAll(x => x.Error > 0 || x.Warning > 0);
                        }
                        gridProcess.ItemsSource = null;
                        gridProcess.ItemsSource = MyOperators_Copy;
                        pnlStatus.DataContext = null;
                        pnlStatus.DataContext = MyStatus_Copy;
                    });
                }
                catch (TaskCanceledException)
                {
                    break;
                }
                Thread.Sleep(1000);
                //if(!MyStatus.Status.HasFlag(Form_Execute_StatusType.已停止) && !MyStatus.Status.HasFlag(Form_Execute_StatusType.错误))
                //{
                //    --times;
                //    if (times <= 0)
                //        break;
                //}
            }
            while (flag);
        }

        public void LoadData()
        {
            btnEncrypt.Visibility = Visibility.Hidden;
            //MouseButtonEventArgs args = new MouseButtonEventArgs(Mouse.PrimaryDevice,
            //          0, MouseButton.Left);
            //args.RoutedEvent = Button.ClickEvent;
            //btnRun.RaiseEvent(args);
            btnRun.IsEnabled = true;
            MyStatus.SetStatus(Form_Execute_StatusType.已停止);
        }
        

        public Form_Execute_Status MyStatus { get; }
        Form_Execute_Status MyStatus_Copy { get; }

        public List<Form_Execute_Operator> MyOperators { get; }

        List<Form_Execute_Operator> MyOperators_Copy { get; set; }

        Form_Execute_Operator _GetOperator(string name)
        {
            return this.MyOperators.FirstOrDefault(x => x.Name == name);
        }
        //MyOperator _GetOperator(表映射 map)
        //{
        //    return this.MyOperators.FirstOrDefault(x => x.Mapping == map);
        //}



        protected void OnPrevious_Click(object o, EventArgs e)
        {
            MainWindow.Current.tab_4.IsEnabled = true;
            MainWindow.Current.tab_4.IsSelected = true;
        }

        protected void OnRun_Click(object o, EventArgs e)
        {
            MyStatus.SetStatus(Form_Execute_StatusType.运行中);
        }

        protected void OnStop_Click(object o, EventArgs e)
        {
            MyStatus.SetStatus(Form_Execute_StatusType.已停止);
        }

        protected void OnReport_Click(object o, EventArgs e)
        {
            Process.Start(MyLogger.Current.LogDir);
        }

        protected void OnEncrypt_Click(object o,EventArgs e)
        {
            Window_Encrypt win = new Forms.Window_Encrypt();
            win.ShowDialog();
        }

        private void Label_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Control ctr = sender as Control;
            string id = ((Form_Execute_Operator)ctr.DataContext).MappdingId;
            string dir = MyLogger.Current.LogDir;
            if (!System.IO.Directory.Exists(dir))
            {
                return;
            }
            string[] files = System.IO.Directory.GetFiles(dir, $"*{id}*", System.IO.SearchOption.TopDirectoryOnly);
            new Window_RunResult(files.ToList()).ShowDialog();
            //ctr.DataContext
            //ctr.Parent
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (_Bind_Tread.IsAlive)
            {

            }
            else
            {
                _Bind_Tread = new Thread(BindData);
                _Bind_Tread.Start();
            }
        }


        private void _Exec_Pnl_Total_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ShowSuccessOnly = false;
            ShowWarningOnly = false;
            ShowErrorOnly = false;
        }

        private void _Exec_Pnl_Error_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ShowSuccessOnly = false;
            ShowWarningOnly = false;
            ShowErrorOnly = true;

        }

        private void _Exec_Pnl_Success_MouseDown(object sender, MouseButtonEventArgs e)
        {

            ShowSuccessOnly = true;
            ShowWarningOnly = false;
            ShowErrorOnly = false;
        }

        private void _Exec_Pnl_Warning_MouseDown(object sender, MouseButtonEventArgs e)
        {

            ShowSuccessOnly = false;
            ShowWarningOnly = true;
            ShowErrorOnly = false;
        }
    }


}
